﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common.Constants;
using Common.DataModel;
using Common.Repository;
using SurveyAutomation.Properties;

namespace SurveyAutomation
{
    public partial class TextAutoManager : Form
    {
        private readonly SqLiteDatabase _repo;

        public TextAutoManager()
        {
            InitializeComponent();
            _repo = new SqLiteDatabase();
        }

        private void TextAutoManager_Load(object sender, EventArgs e)
        {
            // Bind to Commbo
            BindLangData();

            // Bind data to Grid
            BindTextAutoData();
        }

        private void BindLangData()
        {
            var languages = _repo.GetLanguages();
            if (languages != null)
            {
                cboLang.DataSource = languages;
                cboLang.DisplayMember = Constant.LanguageNameFieldName;
                cboLang.ValueMember = Constant.LanguageIdFieldName;
            }
        }

        private void BindTextAutoData()
        {
            var textAutos = _repo.GetTextAutomations();
            if(textAutos != null)
            {
                gvTextAuto.DataSource = textAutos;
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            if(btnAddNew.Text == Resources.Add)
            {
                btnAddNew.Text = Resources.Save;
                btnUpdate.Text = Resources.Cancel;
                gvTextAuto.Enabled = false;

                ClearFormData();
            }
            else
            {
                var langId = int.Parse(cboLang.SelectedValue.ToString());
                if(langId == 0)
                {
                    UtilsHelper.ShowMessage(Resources.Please_select_language_to_add, MessageBoxButtons.OK,
                                            MessageBoxIcon.Exclamation);
                }
                else
                {
                    var returnVal = _repo.InsertTextAutomation(langId, txtTextAuto.Text.Trim());
                    if(returnVal == -1)
                    {
                        UtilsHelper.ShowMessage(Resources.Can_not_save__please_try_again_later_or_contact_administrator, MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                    }
                    else
                    {
                        UtilsHelper.ShowMessage(Resources.Save_successfully, MessageBoxButtons.OK,
                                                MessageBoxIcon.Information);
                        btnAddNew.Text = Resources.Add;
                        btnUpdate.Text = Resources.Update;
                        gvTextAuto.Enabled = true;
                        ClearFormData();

                        BindTextAutoData();
                    }
                }
            }
        }

        private void ClearFormData()
        {
            cboLang.SelectedIndex = 0;
            txtTextAuto.Text = "";
            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if(btnUpdate.Text == Resources.Update)
            {
                if(hiddenId.Text == string.Empty)
                {
                    UtilsHelper.ShowMessage("Please select item in Grid to update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    // update TextAuto
                    var returnVal = _repo.UpdateTextAutomation(int.Parse(hiddenId.Text), txtTextAuto.Text.Trim());
                    if (returnVal != -1)
                    {
                        BindTextAutoData();
                        UtilsHelper.ShowMessage(Resources.Update_successfully, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        UtilsHelper.ShowMessage(Resources.Can_not_save__please_try_again_later_or_contact_administrator, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        ClearFormData();
                    }    
                }
                
            }
            else
            {
                // cancel
                ClearFormData();
            }
        }

        private void gvTextAuto_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (gvTextAuto.SelectedCells != null && gvTextAuto.SelectedCells.Count > 0)
            {
                var i = gvTextAuto.SelectedCells[0].RowIndex;

                hiddenId.Text = gvTextAuto.Rows[i].Cells[0].Value.ToString();
                var lang = _repo.GetLanguageByName(gvTextAuto.Rows[i].Cells[1].Value.ToString());
                if(lang != null)
                {
                    cboLang.SelectedValue = lang.Id;
                }
                
                txtTextAuto.Text = gvTextAuto.Rows[i].Cells[2].Value.ToString();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var dlgResult = UtilsHelper.ShowMessage("Are you sure you want to delete this item",
                                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (dlgResult == DialogResult.Yes)
            {
                var i = gvTextAuto.SelectedCells[0].RowIndex;
                if (i != -1)
                {
                    var textId = gvTextAuto.Rows[i].Cells[0].Value.ToString();
                    // delete
                    var returnVal = _repo.DeleteTextAutomation(int.Parse(textId));
                    if (returnVal == -1)
                    {
                        UtilsHelper.ShowMessage(Resources.Can_not_delete__please_try_again_or_contact_administrator, MessageBoxButtons.OK,
                                            MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        UtilsHelper.ShowMessage(Resources.Delete_successfully, MessageBoxButtons.OK,
                                            MessageBoxIcon.Exclamation);
                        ClearFormData();
                        BindTextAutoData();
                    }
                }
                else
                {
                    UtilsHelper.ShowMessage(Resources.Please_select_language_in_the_grid_to_delete, MessageBoxButtons.OK,
                                            MessageBoxIcon.Information);
                }

            }
        }
    }
}
